package com.css.fxfzfxqh.modules.comprehensiveRiskZoneSet.service;

import cn.hutool.core.util.IdUtil;
import com.css.fxfzfxqh.modules.buildingKilometerGrid.exceptions.BizException;
import com.css.fxfzfxqh.modules.comprehensiveRiskZoneSet.model.entity.FxqhComprehensiveRiskZoneSetSet;
import com.css.fxfzfxqh.modules.comprehensiveRiskZoneSet.model.param.ComprehensiveRiskZoneSetSaveParam;
import com.css.fxfzfxqh.modules.comprehensiveRiskZoneSet.model.vo.ComprehensiveRiskZoneSetSaveVo;
import com.css.fxfzfxqh.modules.comprehensiveRiskZoneSet.repository.ComprehensiveRiskZoneSetRepository;
import com.css.fxfzfxqh.util.PlatformDateUtils;
import com.css.fxfzfxqh.web.PlatformSessionContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class ComprehensiveRiskZoneSetService {
    @Autowired
    private ComprehensiveRiskZoneSetRepository comprehensiveRiskZoneSetRepository;
    @Autowired
    private RedisTemplate redisTemplate;
    // 临界值缓存
    private static final String VALUE_CACHE = "comprehensiveRiskZoneSet:";

    public void save(ComprehensiveRiskZoneSetSaveParam param) throws BizException {
        saveParamCheck(param);
        if (param.getType() == 1) {
            FxqhComprehensiveRiskZoneSetSet fxqhComprehensiveRiskZoneSetSet = new FxqhComprehensiveRiskZoneSetSet();
            fxqhComprehensiveRiskZoneSetSet.setType(1);
            fxqhComprehensiveRiskZoneSetSet.setV1(param.getV1());
            fxqhComprehensiveRiskZoneSetSet.setV2(param.getV2());
            fxqhComprehensiveRiskZoneSetSet.setV3(param.getV3());
            fxqhComprehensiveRiskZoneSetSet.setV4(param.getV4());
            this.saveHandle(fxqhComprehensiveRiskZoneSetSet);
            FxqhComprehensiveRiskZoneSetSet fxqhComprehensiveRiskZoneSetSetXqx = new FxqhComprehensiveRiskZoneSetSet();
            fxqhComprehensiveRiskZoneSetSetXqx.setType(2);
            fxqhComprehensiveRiskZoneSetSetXqx.setV1(param.getXqxV1());
            fxqhComprehensiveRiskZoneSetSetXqx.setV2(param.getXqxV2());
            fxqhComprehensiveRiskZoneSetSetXqx.setV3(param.getXqxV3());
            fxqhComprehensiveRiskZoneSetSetXqx.setV4(param.getXqxV4());
            this.saveHandle(fxqhComprehensiveRiskZoneSetSetXqx);
        }
        if (param.getType() == 2) {
            FxqhComprehensiveRiskZoneSetSet fxqhComprehensiveRiskZoneSetSet = new FxqhComprehensiveRiskZoneSetSet();
            fxqhComprehensiveRiskZoneSetSet.setType(3);
            fxqhComprehensiveRiskZoneSetSet.setV1(param.getV1());
            fxqhComprehensiveRiskZoneSetSet.setV2(param.getV2());
            fxqhComprehensiveRiskZoneSetSet.setV3(param.getV3());
            fxqhComprehensiveRiskZoneSetSet.setV4(param.getV4());
            this.saveHandle(fxqhComprehensiveRiskZoneSetSet);
            FxqhComprehensiveRiskZoneSetSet fxqhComprehensiveRiskZoneSetSetXqx = new FxqhComprehensiveRiskZoneSetSet();
            fxqhComprehensiveRiskZoneSetSetXqx.setType(4);
            fxqhComprehensiveRiskZoneSetSetXqx.setV1(param.getXqxV1());
            fxqhComprehensiveRiskZoneSetSetXqx.setV2(param.getXqxV2());
            fxqhComprehensiveRiskZoneSetSetXqx.setV3(param.getXqxV3());
            fxqhComprehensiveRiskZoneSetSetXqx.setV4(param.getXqxV4());
            this.saveHandle(fxqhComprehensiveRiskZoneSetSetXqx);
        }
        if (param.getType() == 3) {
            FxqhComprehensiveRiskZoneSetSet fxqhComprehensiveRiskZoneSetSet = new FxqhComprehensiveRiskZoneSetSet();
            fxqhComprehensiveRiskZoneSetSet.setType(6);
            fxqhComprehensiveRiskZoneSetSet.setV1(param.getV1());
            fxqhComprehensiveRiskZoneSetSet.setV2(param.getV2());
            fxqhComprehensiveRiskZoneSetSet.setV3(param.getV3());
            fxqhComprehensiveRiskZoneSetSet.setV4(param.getV4());
            fxqhComprehensiveRiskZoneSetSet.setDeathNum(param.getDeathNum());
            fxqhComprehensiveRiskZoneSetSet.setEconomicLoss(param.getEconomicLoss());
            this.saveHandle(fxqhComprehensiveRiskZoneSetSet);
        }
    }

    /**
     * 保存
     *
     * @return
     */
    public void saveHandle(FxqhComprehensiveRiskZoneSetSet param) {
        Date currTime = new Date();
        String currUid = PlatformSessionContext.getUserID();
        FxqhComprehensiveRiskZoneSetSet fxqhComprehensiveRiskZoneSetSet = comprehensiveRiskZoneSetRepository.selectByType(param.getType());
        if (fxqhComprehensiveRiskZoneSetSet == null) {
            // 走新增
            param.setId(IdUtil.getSnowflakeNextIdStr());
            param.setCreateUser(currUid);
            param.setCreateTime(currTime);
            comprehensiveRiskZoneSetRepository.insert(param);
        } else {
            // 更新
            fxqhComprehensiveRiskZoneSetSet.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
            fxqhComprehensiveRiskZoneSetSet.setUpdateUser(PlatformSessionContext.getUserID());
            fxqhComprehensiveRiskZoneSetSet.setV1(param.getV1());
            fxqhComprehensiveRiskZoneSetSet.setV2(param.getV2());
            fxqhComprehensiveRiskZoneSetSet.setV3(param.getV3());
            fxqhComprehensiveRiskZoneSetSet.setV4(param.getV4());
            fxqhComprehensiveRiskZoneSetSet.setDeathNum(param.getDeathNum());
            fxqhComprehensiveRiskZoneSetSet.setEconomicLoss(param.getEconomicLoss());
            comprehensiveRiskZoneSetRepository.updateById(fxqhComprehensiveRiskZoneSetSet);
        }
        redisTemplate.delete(VALUE_CACHE + param.getType());
    }

    /**
     * 保存方法，参数检查
     *
     * @param param
     * @throws BizException
     */
    private void saveParamCheck(ComprehensiveRiskZoneSetSaveParam param) throws BizException {
        if (param.getType() == null) {
            throw new BizException("type不能为空");
        }
        if (param.getType() > 3) {
            throw new BizException("type参数错误");
        }
        if (StringUtils.isBlank(param.getV1())) {
            throw new BizException("临界值1不能为空");
        }
        if (StringUtils.isBlank(param.getV2())) {
            throw new BizException("临界值2不能为空");
        }
        if (StringUtils.isBlank(param.getV3())) {
            throw new BizException("临界值3不能为空");
        }
        if (StringUtils.isBlank(param.getV4())) {
            throw new BizException("临界值4不能为空");
        }
        if (Double.parseDouble(param.getV1()) > Double.parseDouble(param.getV2())) {
            throw new BizException("临界值1不能大于临界值2");
        }
        if (Double.parseDouble(param.getV2()) > Double.parseDouble(param.getV3())) {
            throw new BizException("临界值2不能大于临界值3");
        }
        if (Double.parseDouble(param.getV3()) > Double.parseDouble(param.getV4())) {
            throw new BizException("临界值3不能大于临界值4");
        }
        if (param.getType() == 2) {
            if (StringUtils.isBlank(param.getXqxV1())) {
                throw new BizException("新疆、青海和西藏地区临界值1不能为空");
            }
            if (StringUtils.isBlank(param.getXqxV2())) {
                throw new BizException("新疆、青海和西藏地区临界值2不能为空");
            }
            if (StringUtils.isBlank(param.getXqxV3())) {
                throw new BizException("新疆、青海和西藏地区临界值3不能为空");
            }
            if (StringUtils.isBlank(param.getXqxV4())) {
                throw new BizException("新疆、青海和西藏地区临界值4不能为空");
            }
            if (Double.parseDouble(param.getXqxV1()) > Double.parseDouble(param.getXqxV2())) {
                throw new BizException("新疆、青海和西藏地区临界值1不能大于临界值2");
            }
            if (Double.parseDouble(param.getXqxV2()) > Double.parseDouble(param.getXqxV3())) {
                throw new BizException("新疆、青海和西藏地区临界值2不能大于临界值3");
            }
            if (Double.parseDouble(param.getXqxV3()) > Double.parseDouble(param.getXqxV4())) {
                throw new BizException("新疆、青海和西藏地区临界值3不能大于临界值4");
            }
        }
    }

    public FxqhComprehensiveRiskZoneSetSet selectByType(Integer type) {
        ValueOperations<String, FxqhComprehensiveRiskZoneSetSet> ops = redisTemplate.opsForValue();
        FxqhComprehensiveRiskZoneSetSet fxqhComprehensiveRiskZoneSetSet = ops.get(VALUE_CACHE + type);
        if (fxqhComprehensiveRiskZoneSetSet == null) {
            fxqhComprehensiveRiskZoneSetSet = comprehensiveRiskZoneSetRepository.selectByType(type);
            if (fxqhComprehensiveRiskZoneSetSet != null) {
                ops.set(VALUE_CACHE + type, fxqhComprehensiveRiskZoneSetSet);
                redisTemplate.expire(VALUE_CACHE + type, 10, TimeUnit.HOURS);
            }
        }
        return fxqhComprehensiveRiskZoneSetSet;
    }

    public ComprehensiveRiskZoneSetSaveVo get(Integer type) {
        if (type == 1) {
            return fxqhSingleIndicatorRiskZoneSetEntity2Vo(this.selectByType(1), this.selectByType(2));
        }
        if (type == 2) {
            return fxqhSingleIndicatorRiskZoneSetEntity2Vo(this.selectByType(3), this.selectByType(4));
        }
        if (type == 3) {
            FxqhComprehensiveRiskZoneSetSet entity = this.selectByType(6);
            ComprehensiveRiskZoneSetSaveVo vo = new ComprehensiveRiskZoneSetSaveVo();
            if (entity == null) {
                vo.setV1("0");
                vo.setV2("0");
                vo.setV3("0");
                vo.setV4("0");
                vo.setDeathNum("0");
                vo.setEconomicLoss("0");
            } else {
                vo.setV1(entity.getV1());
                vo.setV2(entity.getV2());
                vo.setV3(entity.getV3());
                vo.setV4(entity.getV4());
                vo.setDeathNum(entity.getDeathNum());
                vo.setEconomicLoss(entity.getEconomicLoss());
            }
            return vo;
        }
        return null;
    }

    private static ComprehensiveRiskZoneSetSaveVo fxqhSingleIndicatorRiskZoneSetEntity2Vo(FxqhComprehensiveRiskZoneSetSet entity, FxqhComprehensiveRiskZoneSetSet xqxEntity) {
        ComprehensiveRiskZoneSetSaveVo vo = new ComprehensiveRiskZoneSetSaveVo();
        if (entity == null) {
            vo.setV1("0");
            vo.setV2("0");
            vo.setV3("0");
            vo.setV4("0");
        } else {
            vo.setV1(entity.getV1());
            vo.setV2(entity.getV2());
            vo.setV3(entity.getV3());
            vo.setV4(entity.getV4());
        }
        if (xqxEntity == null) {
            vo.setXqxV1("0");
            vo.setXqxV2("0");
            vo.setXqxV3("0");
            vo.setXqxV4("0");
        } else {
            vo.setXqxV1(xqxEntity.getV1());
            vo.setXqxV2(xqxEntity.getV2());
            vo.setXqxV3(xqxEntity.getV3());
            vo.setXqxV4(xqxEntity.getV4());
        }
        return vo;
    }
}
